/***************************************************************************
 *   Copyright (C) 2007 by Eduardo Robles Elvira   *
 *   edulix@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QRegExp>
#include <QDebug>
#include "common.h"
#include "eventsmodel.h"
#include "konntrackmodel.h"
#include "konntracksortfiltermodel.h"

bool KonntrackSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
	QString leftString;
	QString rightString;
	EventsModel *eventsSourceModel;
	KonntrackModel *konntrackSourceModel;
	
	if((konntrackSourceModel = qobject_cast<KonntrackModel *>(sourceModel())) != 0) {
		leftString = konntrackSourceModel->data(left, Qt::DisplayRole, false).toString();
		rightString = konntrackSourceModel->data(right, Qt::DisplayRole, false).toString();
	} else if((eventsSourceModel = qobject_cast<EventsModel *>(sourceModel())) != 0) {
		leftString = eventsSourceModel->data(left, Qt::DisplayRole, false).toString();
		rightString = eventsSourceModel->data(right, Qt::DisplayRole, false).toString();
	} else {
		leftString = sourceModel()->data(left).toString();
		rightString = sourceModel()->data(right).toString();
	}
	int col = left.column();
	
	if (col == ORIG_SPORT || col == ORIG_DPORT || col == ORIG_PACKETS || col == ORIG_BYTES || col == REPLY_SPORT || col == REPLY_DPORT || col == REPLY_PACKETS || col == TIMEOUT || col == NUMBER) {
		bool ok;
		int leftInt = leftString.toInt(&ok);
		if(!ok)
			return false;
		int rightInt = rightString.toInt(&ok);
		if(!ok)
			return false;
		return leftInt < rightInt;
	} else if (col == ORIG_SRC_IP || col == ORIG_DST_IP || col == REPLY_SRC_IP || col == REPLY_DST_IP) {
		QRegExp rx("(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)");
		QRegExp rx2("(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)");
		int leftIp[4], rightIp[4];
		int leftPos = rx.indexIn(leftString);
		int rightPos = rx2.indexIn(rightString);
		
		if(leftPos != -1 && rightPos != -1) {
			leftIp[0] = rx.cap(1).toInt();
			leftIp[1] = rx.cap(2).toInt();
			leftIp[2] = rx.cap(3).toInt();
			leftIp[3] = rx.cap(4).toInt();
			rightIp[0] = rx2.cap(1).toInt();
			rightIp[1] = rx2.cap(2).toInt();
			rightIp[2] = rx2.cap(3).toInt();
			rightIp[3] = rx2.cap(4).toInt();
			if(leftIp[0] < rightIp[0])
				return true;
			else
				return false;
			
			if(leftIp[1] < rightIp[1])
				return true;
			else
				return false;
			
			if(leftIp[2] < rightIp[2])
				return true;
			else
				return false;
			
			if(leftIp[3] < rightIp[3])
				return true;
			else
				return false;
		} else {
			qDebug() << leftString << "," << rightString << endl;
			return false;
		}
	} else {
		return QString::localeAwareCompare(leftString, rightString) < 0;
	}
	
	// never reach here
	return false;
}
